# coding:utf-8

import numpy as np


# 06-2 numpy index
# 001
x = np.arange(10, 1, -1)
y = x[np.array([3, 3, 1, 8])]
print(x)
print(y)
# [10  9  8  7  6  5  4  3  2]
# [7 7 9 2]

y = x[np.array([3, 3, -3, 8])]
print(y)
# [7 7 4 2]

y = x[np.array([[1, 1], [2, 3]])]
print(y)
# [[9 9]
#  [8 7]]

# 002
x = np.arange(10, 1, -1)
print(x > 5)
# [ True  True  True  True  True False False False False]

y = x[x > 5]
print(y)
# [10  9  8  7  6]

y = np.arange(35).reshape(5, 7)
print(y)
# [[ 0  1  2  3  4  5  6]
#  [ 7  8  9 10 11 12 13]
#  [14 15 16 17 18 19 20]
#  [21 22 23 24 25 26 27]
#  [28 29 30 31 32 33 34]]
b = y > 20
print(b)
b = b[:, 3]  # 指的是 矩阵b的第3列
print(b)
# [[False False False False False False False]
#  [False False False False False False False]
#  [False False False False False False False]
#  [ True  True  True  True  True  True  True]
#  [ True  True  True  True  True  True  True]]
# [False False False  True  True]


print(y[b])
# [[21 22 23 24 25 26 27]
#  [28 29 30 31 32 33 34]]

# 003
x = np.arange(32).reshape((8, 4))
print(x)
print('----------')
print(x[[4, 2, 1, 7]])
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]
#  [12 13 14 15]
#  [16 17 18 19]
#  [20 21 22 23]
#  [24 25 26 27]
#  [28 29 30 31]]
# ----------
# [[16 17 18 19]
#  [ 8  9 10 11]
#  [ 4  5  6  7]
#  [28 29 30 31]]

x = np.arange(12).reshape(3, 4)
print(x)
print(x[[0, 1], [2]])  #
print(x[[0, 1], [2, 2]])
print(x[[0, 1], 2])
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
# [2 6]
# [2 6]

print('******')
print(x[0, [0]])
print(x[[0], [0]])
print(x[0, 0])
# ******
# [0]
# [0]
# 0
